import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/widgets/sms_verify_dialog.dart';
import 'package:flutter_deer/account/widgets/withdrawal_password_setting.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/base_dialog.dart';
import 'package:flutter_deer/widgets/click_item.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:oktoast/oktoast.dart';

class WithdrawalPasswordPage extends StatefulWidget {

  const WithdrawalPasswordPage({super.key});

  @override
  _WithdrawalPasswordPageState createState() => _WithdrawalPasswordPageState();
}

class _WithdrawalPasswordPageState extends State<WithdrawalPasswordPage> {

  String? phone = '';
  bool? existWithdrawPassword = false;

  @override
  void initState() {
    super.initState();
    this.phone = SpUtil.getString(Constant.phone).nullSafe;
    this.existWithdrawPassword = SpUtil.getBool(Constant.existWithdralPassword);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
                appBar: MyAppBar(
                  centerTitle: '${DeerLocalizations.of(context)!.withdrawPassword.nullSafe}',
                ),
                body: Column(
                  children: <Widget>[
                    Gaps.vGap5,
                    ClickItem(
                        title: existWithdrawPassword==true?'${DeerLocalizations.of(context)!.changePassword.nullSafe}':'${DeerLocalizations.of(context)!.settingPassword.nullSafe}',
                        onTap: () {
                            showModalBottomSheet<void>(
                                context: context,

                                /// 禁止拖动关闭
                                enableDrag: false,

                                /// 使用true则高度不受16分之9的最高限制
                                isScrollControlled: true,
                                builder: (
                                    _) => WithdrawalPasswordSetting(existWithdrawPassword:existWithdrawPassword)
                            ).then((value){
                              existWithdrawPassword = SpUtil.getBool(Constant.existWithdralPassword);
                            });
                        }
                    ),
                    ClickItem(
                        title:  '${DeerLocalizations.of(context)!.forgotPasswordLink.nullSafe}',
                        onTap: (){
                            if (existWithdrawPassword == true) {
                              _showHintDialog();
                            } else {
                              showToast('${DeerLocalizations.of(context)!.noWithdrawalPasswordSet.nullSafe}');
                            }
                        }
                    ),
                  ],
                ),
              );
  }

  void _showHintDialog() {
    showElasticDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return BaseDialog(
          hiddenTitle: true,
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.0),
            child: Text('${DeerLocalizations.of(context)!.securityOfYourAccount.nullSafe}', textAlign: TextAlign.center),
          ),
          onPressed: () {
            NavigatorUtils.goBack(context);
            _showVerifyDialog();
          },
        );
      },
    );
  }

  void _showVerifyDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (_)=> SMSVerifyDialog(phone: phone.nullSafe)

    );
  }
}
